package com.cssw.fyzb.tps.api.adapay;

import com.alibaba.fastjson.JSON;
import com.cssw.fyzb.tps.config.adapay.AdapayConfig;
import com.huifu.adapay.model.Member;
import com.huifu.adapay.model.SettleAccount;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: yangyiqun
 * @Date: 2021/1/06 22:29
 */
@Function(description = "创建adapay结算账户", parameters = {
        @Parameter(name = "userId", type = Type.String, description = "用户标识", required = true),
        @Parameter(name = "bankNo", type = Type.String, description = "银行卡号", required = true),
        @Parameter(name = "userName", type = Type.String, description = "用户姓名", required = true),
        @Parameter(name = "idCard", type = Type.String, description = "身份证号码", required = true),
        @Parameter(name = "mobile", type = Type.String, description = "手机号码", required = true),
        @Parameter(name = "bankCode", type = Type.String, description = "银行卡号", required = true),
        @Parameter(name = "bankName", type = Type.String, description = "开户行名称", required = true),
        @Parameter(name = "province", type = Type.String, description = "省份编码", required = true),
        @Parameter(name = "area", type = Type.String, description = "市编码", required = true),
})
@Component
public class CreateSettleAcctAdapay implements Action {

    private static final Logger logger = LoggerFactory.getLogger(CreateSettleAcctAdapay.class);

    @Autowired
    private AdapayConfig adapayConfig;

    @Override
    public Response execute(Request request) throws Exception {
        String userId = request.getString("userId");
        String bankNo = request.getString("bankNo");
        String userName = request.getString("userName");
        String idCard = request.getString("idCard");
        String mobile = request.getString("mobile");
        String bankCode = request.getString("bankCode");
        String bankName = request.getString("bankName");
        String province = request.getString("province");
        String area = request.getString("area");

        try {
            Map<String, Object> settleCountParams = new  HashMap<String, Object>();
            Map<String, Object> accountInfo = new  HashMap<String, Object>();
            accountInfo.put("card_id",bankNo);
            accountInfo.put("card_name",userName);
            accountInfo.put("cert_id",idCard);
            accountInfo.put("cert_type","00");
            accountInfo.put("tel_no",mobile);
            accountInfo.put("bank_code", bankCode);
            accountInfo.put("bank_name", bankName);
            accountInfo.put("bank_acct_type","2");
            accountInfo.put("prov_code", province);
            accountInfo.put("area_code",area);
            settleCountParams.put("member_id", userId);
            settleCountParams.put("app_id", adapayConfig.getAppId());
            settleCountParams.put("channel","bank_account");
            settleCountParams.put("account_info", accountInfo);
            System.out.println(JSON.toJSONString(settleCountParams));
            Map<String, Object> settleCount = SettleAccount.create(settleCountParams);

            return Response.success().put("data", settleCount);
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("创建结算账户失败");
        }
    }
}
